package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysObject;
import com.cjb.cloudsystem.service.SysObjectService;
import com.cjb.cloudsystem.vo.FileVO;
import com.cjb.cloudsystem.vo.SysObjectVO;
import com.cjb.common.util.Common;
import com.cjb.common.util.JsonUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author 1.0.0 2023-10-22
 * @Package IntelliJ IDEA
 * @Class SysObjectControl
 * @Date 2023-10-22
 */
@RestController
@Tag(name = "sysObject")
@RequestMapping(value = {"sys/object"})
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@Slf4j
public class SysObjectController {
    /*
     * 注入事物类
     */
    @Autowired
    private SysObjectService sysObjectService;

    /**
     * 单个保存
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @PostMapping("/save")
    @Operation(summary = "单个保存")
    public Result<Boolean> save(@RequestBody SysObject sysObject) throws Exception {
        if (Common.isEmpty(sysObject.getSysOrgId())){
            sysObject.setSysOrgId("-999");
        }
        return Result.success(sysObjectService.save(sysObject));
    }

    /**
     * 批量保存
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result<Integer> listSave(@RequestBody List<SysObject> sysObject) {
        return Result.success(sysObjectService.listSave(sysObject));
    }

    /**
     * 单个查询
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @GetMapping("/queryById")
    @Operation(summary = "单个查询-通过ID")
    public Result<SysObject> queryById(@RequestBody SysObject sysObject) {
        return Result.success(sysObjectService.queryById(sysObject));
    }

    /**
     * 分页查询
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @GetMapping("/queryPage")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<SysObject>> queryPage(@ParameterObject @Valid SysObjectVO sysObjectVO) {
        log.info("get page {}",sysObjectVO.getPage());
        Page sysObjectPage = new Page<SysObjectVO>(sysObjectVO.getPage(),sysObjectVO.getLimit());
        log.info("查看页面对象 {}",JsonUtils.toJsonString(sysObjectPage));
        return Result.success(sysObjectService.queryPage(sysObjectPage, sysObjectVO));
    }

    /**
     * 批量更新
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result<String> listUpdate(@RequestBody List<SysObject> sysObject) {
        log.info("打印传入数据 {}",JsonUtils.toJsonString(sysObject.get(0)));
        sysObjectService.listUpdate(sysObject);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @DeleteMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result<String> listDelete(@RequestBody List<SysObject> sysObject) {
        sysObjectService.listDelete(sysObject);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @PostMapping("/deleteById")
    @Operation(summary = "单个删除-通过ID")
    public Result<String> deleteById(@RequestBody SysObject sysObject) {
        sysObjectService.deleteById(sysObject);
        return Result.success();
    }

    /**
     * 返回全部信息
     *
     * @Author 1.0.0 2023-10-22
     * @Value
     * @Title
     */
    @PostMapping("/returnAllData")
    @Operation(summary = "返回所有信息")
    public Result<List<SysObject>> returnAllData() {
        return Result.success(sysObjectService.returnAllData());
    }

    /**
     * 读取hdfs数据
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("readFile")
    @Operation(summary = "读取文件信息")
    public void readFile(String filePath) {
        sysObjectService.readFile(filePath);
    }

    /**
     * 上传
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("upload")
    @Operation(summary = "上传")
    public Result<FileVO> upload(MultipartFile file, String dir) throws Exception {
        log.info("文件接受");
        return Result.success(sysObjectService.upload(file, dir));
    }
    /**
     * 下载
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("download")
    @Operation(summary = "下载")
    public void download(String filePath) {
        log.info("打印接受参数{}", JsonUtils.toJsonString(filePath));
        sysObjectService.downLoad(filePath);
    }
}
